package com.xm.xmapp;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.Constants;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.http.UpdataAppClass.HandlerInterFace;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.RegisterRequest;
import com.xm.xmapp.request.bean.Send_smsRequest;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.Des4;
import com.xm.xmapp.tools.MyHanler;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class RegisterActivity extends BaseActivity implements OnClickListener {

	private TextView chuangjian, getyanzhengma, denglu, url;
	private ImageView back, img, isvisable;
	private EditText phone, yanzhengma, password;
	private int time = 60;
	private Timer timer;
	private boolean isVisible;
	private MyHanler handler;
	private boolean flag;// 是否选择同意一步用车协议

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

	}

	@Override
	public void initView() {

		chuangjian = (TextView) findViewById(R.id.chuangjian);
		getyanzhengma = (TextView) findViewById(R.id.getyanzhengma);
		denglu = (TextView) findViewById(R.id.denglu);
		back = (ImageView) findViewById(R.id.back);
		phone = (EditText) findViewById(R.id.phone);
		yanzhengma = (EditText) findViewById(R.id.yanzhengma);
		password = (EditText) findViewById(R.id.password);
		url = (TextView) findViewById(R.id.url);
		img = (ImageView) findViewById(R.id.img);
		isvisable = (ImageView) findViewById(R.id.isvisable);

	}

	@Override
	public void initData() {

		getyanzhengma.setBackgroundResource(R.drawable.myborder1);
		handler = new MyHanler(new HandlerInterFace() {

			@Override
			public void run(Message msg) {

				time--;
				getyanzhengma.setText(time + "秒");
				if (time == 0) {
					getyanzhengma.setClickable(true);
					getyanzhengma.setBackgroundResource(R.drawable.myborder1);
					getyanzhengma.setText("点击重新获取验证码");
					time = 60;
					timer.cancel();
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (timer != null)
			timer.cancel();
	}

	@Override
	public void initEvent() {

		TextWatcher textWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				setBg();
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		};

		yanzhengma.addTextChangedListener(textWatcher);

		password.addTextChangedListener(textWatcher);
		phone.addTextChangedListener(textWatcher);
		getyanzhengma.setOnClickListener(this);
		isvisable.setOnClickListener(this);
		denglu.setOnClickListener(this);
		back.setOnClickListener(this);
		url.setOnClickListener(this);
		img.setOnClickListener(this);

	}

	protected void setBg() {
		if (flag && !yanzhengma.getText().toString().trim().equals("")
				&& !password.getText().toString().trim().equals("") && !phone.getText().toString().trim().equals("")) {
			chuangjian.setBackgroundResource(R.drawable.onclickbackground1);
			chuangjian.setTextColor(Color.parseColor("#3c3c3c"));
			chuangjian.setOnClickListener(this);
		} else {
			chuangjian.setBackgroundResource(R.drawable.myborder);
			chuangjian.setTextColor(Color.parseColor("#ffffff"));
			chuangjian.setOnClickListener(null);

		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.isvisable:
			isVisible = !isVisible;
			if (isVisible) {
				isvisable.setImageResource(R.mipmap.sign_icon_xianshi);
				password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				isvisable.setImageResource(R.mipmap.sign_icon_yincang);
				password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}

			break;

		case R.id.img:
			flag = !flag;
			setImg();
			setBg();
			break;

		case R.id.url:

			intent = new Intent(MyAppcation.getMyAppcation(), ShowURLActivity.class);
			intent.putExtra("url", Constants.User_agreement);
			intent.putExtra("title", "用户协议");
			startActivity(intent);

			break;

		case R.id.getyanzhengma:
			if (StringTools.isPhone(phone.getText().toString())) {
				Send_smsRequest requestBean = new Send_smsRequest();
				requestBean.phone = phone.getText().toString();
				requestBean.type = 1;
				promptDialog.show();
				MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {

						promptDialog.dismiss();
						getyanzhengma.setClickable(false);
						timer = new Timer();
						timer.scheduleAtFixedRate(new TimerTask() {
							public void run() {
								handler.sendEmptyMessage(0);
							}
						}, 10, 1000);
						getyanzhengma.setBackgroundResource(R.drawable.myborder4);

					}
				}, new DisssmissInterFace() {

					@Override
					public void putError(VolleyError error) {
						promptDialog.dismiss();
					}
				});

			} else {
				Tools.showMessage("输入的手机号不正确,请重新输入");
			}
			break;
		case R.id.chuangjian:

			if (flag) {
				if (!StringTools.isLength(yanzhengma.getText().toString(), 4)) {
					Tools.showMessage("你的验证码长度不对");
				} else if (!StringTools.isLength(password.getText().toString(), 6)) {
					Tools.showMessage("密码至少6位");
				} else {
					RegisterRequest requestBean = new RegisterRequest();
					requestBean.jpush_id = MyAppcation.getMyAppcation().getJpush_id();
					requestBean.phone = phone.getText().toString();
					requestBean.password = Des4.encode(password.getText().toString());
					requestBean.code = yanzhengma.getText().toString();
					requestBean.phone_model = Tools.getMODEL();
					requestBean.phone_system_version = Tools.getSYSTEM_MODEL();
					promptDialog.show();
					MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

						@Override
						public void onResponse(String response) {

							promptDialog.dismiss();
							ResponseUserBean bean = JSON.parseObject(response, ResponseUserBean.class);
							MyAppcation.getMyAppcation().saveUserBean(bean);

							intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
							intent.putExtra("isFrom", 1);
							startActivity(intent);

						}
					}, new DisssmissInterFace() {

						@Override
						public void putError(VolleyError error) {

							promptDialog.dismiss();
						}
					});

				}
			} else {

				Tools.showMessage("请选择同意一步用车协议");
			}

			break;
		case R.id.denglu:
			intent = new Intent(MyAppcation.getMyAppcation(), LoginActivity.class);
			startActivity(intent);
			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

	private void setImg() {

		if (flag)
			img.setImageResource(R.mipmap.tex_nor);
		else
			img.setImageResource(R.mipmap.tex_sel);
	}

}
